<?php
namespace Cyfy\Modules\Comments\Forms;

use \Cyfy\Modules\Forms;
use \Cyfy\Modules\Users\Current;
use \Cyfy\Modules\Comments\Module;
use \Cyfy\Modules\BasicContent\Content;
/**
 * Creating new entry form.
 * 
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class NewEntry extends Forms\Form
{
    /**
     * Structure of the form.
     */
    public function structure()
    {
        //$this -> action = $this -> getTemp("action");
        $this -> onSubmit = "Pager.loadPage('Comments','entries', '#comments',  'commentModule=" . $this -> getTemp("module"). "&commentId=" . $this -> getTemp("id"). "', 'comments', '0'); Cyfy.redirectToOutside('#comments-0'); $('.commentsNewCollapsibleForm').slideUp();";
        $this -> permission("Comments", "add new");
        
        // If user not logged, show input name
        if(Current :: $uid == 0)
        {
            $i = new Forms\TextBox("user", self :: t(110));
            $i -> maxlength = 255;
            $i -> required = true;
            $this -> addItem($i);
        }
        
        
        $i = new Forms\TextBox("title", self :: t(120));
        $i -> maxlength = 255;
        $i -> required = true;
        $this -> addItem($i);
        

        $i = new Forms\TextArea("text",  self :: t(130));
        $i -> required = true;
        $this -> addItem($i);
        
        $b = new Forms\Button(self :: t(140));
        $this -> addButton($b);
        
    }
   
    /**
     * Submitting
     */
    public function submit()
    {
        $values["module"] = $this -> getTemp("module");
        $values["id"] = $this -> getTemp("id");
        
        $c = new Content($this -> get("title"), "<p>" . $this -> get("text") . "</p>");
        $bcid = $c -> save();
        
        $values["bcid"] = $bcid;
        if(Current ::$uid == 0)
            $values["user"] = $this -> get("user");
        else
            $values["user"] = Current :: $name;
            
        
        if(\dibi :: query("INSERT INTO [:cyfy:Comments]", $values))
        {
            \Cyfy\Message :: set(self :: t(101, array($this -> get("title"))));
        }
        else
        {
            \Cyfy\Message :: set(self :: t(102, array($this -> get("title"))), \Cyfy\Message::ERROR);
        }
        return true;
    }
}